package com.example.controller;

import com.example.common.Result;
import com.example.entity.Orders;
import com.example.service.OrdersService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrdersController {
    @Resource
     private OrdersService ordersService;
    @RequestMapping("/insertOrder")
    public Result insertOrder(@RequestParam(value = "orderType")  String orderType){
////        System.out.println("orders = " + orders.getSetmealId());
//        Orders orders1 = new Orders();
//        orders1.setSetmealId(1);
//        orders1.setMemberId(7);
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = "2022-03-11 00:00:00";
//        Date parse = formatter.parse(time);
//        System.out.println("parse = " + parse);
//        orders1.setOrderDate(parse);
//        orders1.setOrderType("电话预约");
        System.out.println("orders = " +orderType);

        return Result.success(null);
    }
    @PostMapping
    public Result<?> save(@RequestBody  Orders orders) {
        System.out.println("orders ======= " + orders.getMemberId());
        System.out.println("orders ======= " + orders.getSetmealId());
        return Result.success(ordersService.save(orders));
    }

    @PutMapping
    public Result<?> update(@RequestBody Orders orders) {
        return Result.success(ordersService.updateById(orders));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        ordersService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Orders> findById(@PathVariable Long id) {
        return Result.success(ordersService.getById(id));
    }

    @GetMapping
    public Result<List<Orders>> findAll() {
        return Result.success(ordersService.list());
    }

//    @GetMapping("/page")
//    public Result<IPage<Orders>> findPage(@RequestParam(required = false, defaultValue = "") String name,
//                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
//                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
//        return Result.success(ordersService.page(new Page<>(pageNum, pageSize), Wrappers.<Orders>lambdaQuery().like(Orders::getName, name)));
//    }

}